var _extends = Object.assign || function(target) {
    for (var i = 1; i < arguments.length; i++) {
        var source = arguments[i];
        for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
                target[key] = source[key];
            }
        }
    }
    return target;
};

// pages/home/search_address.js

// var _underscore = require("../../xcx_base_lib/utils/underscore.js");

// var _underscore2 = _interopRequireDefault(_underscore);

// var _util = require("../../utils/util.js");

// var _util2 = _interopRequireDefault(_util);

// var _uplus = require("../../lib/uplus.js");

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}

function _toConsumableArray(arr) {
    if (Array.isArray(arr)) {
        for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
            arr2[i] = arr[i];
        }
        return arr2;
    } else {
        return Array.from(arr);
    }
}

var app = getApp();





Page({
    /**
   * 页面的初始数据
   */
    data: {
        isStart: true,
        queryText: "",
        searchResult: [],
        currentPosition: {},
        isFirst: true,
        iconON: "/resources/images/on_station.png",
        iconOFF: "/resources/images/off_station.png",
        parameter: {
            'navbar': '1',
            'return': '1',
            'title': '地点搜索',
            'color': true,
            'class': '0'
          },
        latitude :'',
        longitude :''
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function onLoad(options) {
        var _this = this;
   
        this.setData({
            searchType: options.type,
        });
    },
    //清空输入框的类容
    onClearInputText: function onClearInputText() {
        this.setData({
            queryText: "",
            searchAddressRes: []
        });
    },
    //选择搜索结果的站点
    onAddressSelected: function onAddressSelected(e) {
        var data = e.currentTarget.dataset;

        console.log(e.currentTarget)
        if(data.type=='start'){
        wx.setStorage({ key: 'start_position', data: {name:data.name,lat:data.lat,lng:data.lng}});
        }else{
            wx.setStorage({ key: 'end_position', data: {name:data.name,lat:data.lat,lng:data.lng}});
        }

        wx.switchTab({
            url: "/pages/index/index" 
        })
        
    },
    
    //查询站点信息
    onSearchStation: function onSearchStation(e) {

        wx.showLoading({
            title: ""
        });
        var that = this
        this.setData({
                queryText: e.detail.value
            },function(){
                var selectedCity = that.data.currentPosition? that.data.currentPosition.city:'贵阳市';
                var queryText = that.data.queryText.replace(/\s+/g, "");
                app.baseGet(app.U({ c: 'public_api', a:'get_search_position', q: { 
                    city:selectedCity,
                    keyword:queryText,
                    lat: that.data.currentPosition.lat,
                    lng:that.data.currentPosition.lng
                }}),function(res){
                    console.log(res)
                    wx.hideLoading();
                    if(res.code==200){
               
                    that.setData({
                        searchResult: res.data.data
                    });
                    } else{
                        that.setData({
                            searchResult: []
                        });
                    }
                });
            })
         console.log(that.data.searchResult)
        
        //
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function onReady() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function onShow(options) {
        var _this3 = this;

        //获取地理位置
        wx.getLocation({
            type: 'wgs84',
            success: function(res) {
                _this3.getCurrentPosition(res.latitude,res.longitude);   
            },
              fail (res) {
              console.log(res)
            }
          })

    },
        /**
   * 获取用户当前位置
   */
  
  getCurrentPosition: function getCurrentPosition(latitude,longitude) {
      
    var that = this
   

       if(latitude&&longitude){
       app.baseGet(app.U({ c: 'public_api', a:'get_user_location', q: { latitude:latitude,longitude:longitude}}),function(res){
        console.log(res)
        if(res.code==200){
            that.setData({
            currentPosition: {
                address:res.data.result.address,
                title: res.data.result.formatted_addresses.recommend,
                location: res.data.result.location,
                city:  res.data.result.ad_info.city,
                lat: res.data.result.location.lat,
                lng: res.data.result.location.lng,
            }
        });
        } else{
            that.setData({
                currentPosition: []
            });
        }
    });
}else{
    wx.showToast({
        title: '获取位置失败',
        icon: 'none',
        duration: 1000,
        mask: true,
      });
}
},
    
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function onHide() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function onUnload() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function onPullDownRefresh() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function onReachBottom() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function onShareAppMessage() {}
});